package com.smily.excel;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

import java.io.File;

/**
 * JAX 创建EXCEL文件
 * Created by Smily on 10/17/15.
 */
public class JxlExpExecl {

    public static void main(String[] args) {
        //创建EXCEL文件
        File file = new File("/Users/Smily/Documents/Coding/idae_output/jxl_test.xls");
        try {
            file.createNewFile();
            //创建工作簿
            WritableWorkbook writableWorkbook = Workbook.createWorkbook(file);
            //创建sheet
            WritableSheet sheet = writableWorkbook.createSheet("sheet1", 0);
            Label label = null;
            //表头
            String[] title = {"id", "name", "sex"};
            for (int i = 0; i < title.length; i++) {
                label = new Label(i, 0, title[i]);
                sheet.addCell(label);
            }

            //追加数据
            for (int i = 0; i < 10; i++) {
                label = new Label(0, i, "a" + i);
                sheet.addCell(label);
                label = new Label(1, i, "user" + i);
                sheet.addCell(label);
                label = new Label(2, i, "男");
                sheet.addCell(label);
            }

            //写入数据
            writableWorkbook.write();
            writableWorkbook.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
